package cn.kgc.eduservice.controller;


import cn.kgc.commonutils.R;
import cn.kgc.eduservice.client.VodClient;
import cn.kgc.eduservice.entity.EduVideo;
import cn.kgc.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-06
 */
@Api(tags="课程小节")
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;
    //添加小节
    @ApiOperation(value = "新增小节")
    @PostMapping("addVide")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return  R.ok();
    }
    //删除小节 删除对应阿里云视频
    @ApiOperation(value = "删除小节")
    @DeleteMapping("{id}")
    public R dolVideo(@PathVariable String id){
        // 根据小节id 获取视频id， 调用方法实现视频删除
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        //判断小节里面是否有视频id
        if (!StringUtils.isEmpty(videoSourceId)){
            // 根据视频id，远程调用实现视频删除
            vodClient.removeAlyVideo(videoSourceId);
        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }
    //TODO 修改
}

